<?php
include("../../tools/session.php");
include("../../tools/controls.php");

// 如果登录失败、无权限访问，那么采取相应措施
if (!($is_login_success === true)) { session_hop_page("../index.php"); exit(); }
if (!power_check(0)) { include("tpl-inc-wp.php"); $tpl->display($oa_tpl_path . "/no-power.html"); exit(); }

/*-- 以下是实际内容 --*/
$page_status = $_POST["page_status"];
   
// 如果是普通情况，$page_status 为空，则显示页面
if (!$page_status || $page_status == "")
{
	include("../../tools/mysql.php");
	include("tpl-inc-wp.php");
	db_connect();
   
	$doc_id = $_GET["doc_id"];
	if (empty($doc_id)) { $tpl->assign("msg", "参数不足！"); $tpl->display($error_path); exit(); }

	// 如果这个文件不是发往本人的
	$sql = "SELECT * FROM doc_exam WHERE doc_id=" . $doc_id . " AND handout_id=" . $_SESSION["user"]["id"];
	if (!db_query_exists($sql)) { $tpl->assign("msg", "非法访问！"); $tpl->display($error_path); exit(); }

	$sql = "SELECT * FROM document WHERE doc_id=" . $doc_id . " AND is_finished=0";
	$sql2 = "SELECT * FROM doc_exam WHERE doc_id=" . $doc_id . " AND handout_id=" . $_SESSION["user"]["id"] . " AND has_examed=1";
	// 如果这个文件已经审批结束的
	if (!db_query_exists($sql) || db_query_exists($sql2))
	{
		$tpl->assign("msg", "这份公文已经审批过！");
		$tpl->display($error_path);
		exit();
	}
	$re = db_query_once($sql);
	
	// 获取撰写人
	$sql2 = "SELECT username FROM user WHERE id=" . $re["from_id"];
	$re2 = db_query_once($sql2);
	
	// 获取公文模板类型
	$sql3 = "SELECT a.*, b.zh_name class_name FROM doc_tpl a LEFT JOIN doc_tpl_class b ON a.tpl_class_id=b.tpl_class_id WHERE a.tpl_id=" . $re["tpl_id"];
	$re3 = db_query_once($sql3);
	
	// 查看本人是否是多人审批中参与审批者中的一员
	$sql4 = "SELECT count(doc_id) num FROM doc_exam WHERE doc_id=" . $doc_id . " AND
		exam_level=(SELECT exam_level FROM doc_exam WHERE doc_id=" . $doc_id . " AND
		handout_id=" . $_SESSION["user"]["id"] . ")";
	$re4 = db_query_once($sql4);

	$doc_title = $re["title"];				 // 公文标题
	$doc_content = str_replace('\\"', '"', $re["content"]);			// 公文内容
	$doc_time = explode(" ", $re["apply_time"]);
	$doc_time = $doc_time[0];				  // 公文提交时间
	$doc_accessory = explode(";;", $re["accessory"]);
	$doc_tpl_class = $re3["class_name"];
	$doc_tpl = $re3["name"];
	$doc_from = $re2["username"];
	$doc_instancy = $re["instancy"];
	$doc_ismulti = $re["is_multi"];
	$doc_mulpnt = $re["exam_multi_point"];
	$ispartner = ($re4["num"] > 1) ? 1 : 0;

	$tpl->assign("session_id", $_SESSION["user"]["id"]);
	$tpl->assign("doc_id", $doc_id);
	$tpl->assign("doc_title", $doc_title);
	$tpl->assign("doc_content", $doc_content);
	$tpl->assign("doc_time", $doc_time);
	$tpl->assign("doc_accessory", $doc_accessory);
	$tpl->assign("doc_tpl_class", $doc_tpl_class);
	$tpl->assign("doc_tpl", $doc_tpl);
	$tpl->assign("doc_from", $doc_from);
	$tpl->assign("doc_instancy", $doc_instancy);
	$tpl->assign("doc_ismulti", $doc_ismulti);
	$tpl->assign("doc_mulpnt", $doc_mulpnt);
	$tpl->assign("ispartner", $ispartner);
	$tpl->display($oa_tpl_path . "/default/wf_examin2.html");
}
// 当 $page_status == 1 时，提交数据
elseif ($page_status == 1)
{
	include("../../tools/mysql.php");
	db_connect();
	
	$alert_str = array("Success：数据提交成功！", "Error：数据提交失败！");
	
	$doc_id					= $_POST["doc_id"];
	$user_id				= $_SESSION["user"]["id"];
	$is_multi_flag			= $_POST["is_multi_flag"];
	$doc_mulpnt			= $_POST["doc_mulpnt"];
	$doc_receiver_list		= $_POST["doc_receiver_list"];
	$doc_receiver_array		= explode(",", $doc_receiver_list);
	$doc_receiver_num		= count($doc_receiver_array);
	$doc_next_receiver		= $_POST["doc_next_receiver"];
	$ispartner				= $_POST["ispartner"];
	$goon_or_finish			= $_POST["goon_or_finish"];
	$exam_opinion			= $_POST["exam_opinion"];
	$input_opinion			= $_POST["input_opinion"];
	
	// 重命名
	$examin_opinion		= $exam_opinion;
	$examin_content		= $input_opinion;
	$to_id					= $doc_next_receiver;
	
	// 查询数据库，取得目前的审批级数
	$sql = "SELECT exam_level FROM document WHERE doc_id=" . $doc_id;
	$re = db_query_once($sql);
	$exam_level = $re["exam_level"];
	
	// 单人审批
	if ($is_multi_flag == 0)
	{
		// 继续流转
		if ($goon_or_finish == 1)
		{
			// 更新表 document：
			$sql_1 = "UPDATE document "
				. " SET exam_level=" . ($exam_level + 1) . ", "
				. " to_id=" . $to_id . ", "
				. " exam_flow=CONCAT(exam_flow, '->', '" . $to_id . "') "
				. " WHERE doc_id=" . $doc_id;
			// 更新表 doc_exam：
			$sql_2 = "UPDATE doc_exam SET opn_id=" . $examin_opinion . ", content='" . $examin_content . "', "
				. " has_examed=1, exam_time=NOW() "
				. " WHERE doc_id=" . $doc_id . " AND handout_id=" . $user_id;
			// 添加数据到 doc_exam：
			$sql_3 = "INSERT INTO doc_exam(doc_id, handout_id, opn_id, content, has_examed, exam_level, exam_time) "
				. " VALUES(" . $doc_id . ", " . $to_id . ", 0, '', 0, " . ($exam_level + 1) . ", '0000-00-00')";
			echo (db_exec($sql_1) && db_exec($sql_2) && db_exec($sql_3)) ? $alert_str[0] : $alert_str[1]; 
		}
		// 结束审批
		elseif ($goon_or_finish == 9)
		{
			// 更新表 document：
			$sql_1 = "UPDATE document SET is_finished=1, is_passed_at_last=" . ($examin_opinion == 1 ? 1 : 0) . " WHERE doc_id=" . $doc_id;
			// 更新表 doc_exam：
			$sql_2 = "UPDATE doc_exam SET opn_id=" . $examin_opinion . ", content='" . $examin_content . "', "
				. " has_examed=1, exam_time=NOW()"
				. " WHERE doc_id=" . $doc_id . " AND handout_id=" . $user_id;
			echo (db_exec($sql_1) && db_exec($sql_2)) ? $alert_str[0] : $alert_str[1];
		}
	}
	// 多人审批
	else
	{
		// 继续流转
		if ($goon_or_finish == 1)
		{
			// 首个审批者
			if ($ispartner == 0)
			{
				$sql_receiver_list = array();
				foreach ($doc_receiver_array as $v)
				{
					$sql_receiver_list[] = "(" . $doc_id . ", " . $v . ", 0, '', 0, " . $exam_level . ", '0000-00-00')";
				}
				// 更新表 document：
				$sql_1 = "UPDATE document "
					. " SET goon_or_finish=1, to_id=" . $to_id . ", exam_multi_point=" . $doc_mulpnt . ", "
					. " people_num=" . $doc_receiver_num . ", is_multi=1, "
					. " exam_flow=CONCAT(exam_flow, '->', '(" . $doc_receiver_list . ")') "
					. " WHERE doc_id=" . $doc_id;
				// 更新表 doc_exam：
				$sql_2 = "UPDATE doc_exam SET opn_id=" . $examin_opinion . ", content='" . $examin_content . "', "
					. " has_examed=1, exam_time=NOW() "
					. " WHERE doc_id=" . $doc_id . " AND handout_id=" . $user_id;
				// 添加数据到 doc_exam：
				$sql_3 = "INSERT INTO doc_exam(doc_id, handout_id, opn_id, content, has_examed, exam_level, exam_time) "
					. " VALUES" . implode(", ", $sql_receiver_list);
				echo (db_exec($sql_1) && db_exec($sql_2) && db_exec($sql_3)) ? $alert_str[0] : $alert_str[1]; 
			}
			// 审批参与者
			else
			{
				// 更新数据表 doc_exam：
				$sql_1 = "UPDATE doc_exam SET opn_id=" . $examin_opinion . ", content='" . $examin_content . "', "
					. " has_examed=1, exam_time=NOW() "
					. " WHERE doc_id=" . $doc_id . " AND handout_id=" . $user_id;
				echo db_exec($sql_1) ? $alert_str[0] : $alert_str[1];
				
				$sql_temp1 = "SELECT people_num, to_id, goon_or_finish, exam_multi_point FROM document WHERE doc_id=" . $doc_id;
				$sql_temp2 = "SELECT count(doc_id) num FROM doc_exam WHERE doc_id=" . $doc_id . " AND exam_level=" . $exam_level . " AND has_examed=1";
				$sql_temp3 = "SELECT count(opn_id) num FROM doc_exam WHERE doc_id=" . $doc_id . " AND exam_level=" . $exam_level . " AND opn_id=1";
				$re_temp1 = db_query_once($sql_temp1);
				$re_temp2 = db_query_once($sql_temp2);
				$re_temp3 = db_query_once($sql_temp3);
				
				$n_people_num = $re_temp1["people_num"];
				$n_to_id = $re_temp1["to_id"];
				$n_goon_or_finish = $re_temp1["goon_or_finish"];
				$n_exam_multi_point = $re_temp1["exam_multi_point"];
				$n_num_of_examed = $re_temp2["num"];
				$n_num_of_agree = $re_temp3["num"];
				
				$large_than = (($n_num_of_agree / $n_people_num) >= $n_exam_multi_point) ? true : false;
				
				// 如果多人审批结束时
				if ($n_people_num == $n_num_of_examed)
				{
					// 当未通过或者该流程至此结束时
					if (!$large_than || $n_goon_or_finish == 9)
					{
						// 更新表格 document：
						$sql_1 = "UPDATE document SET is_finished=1, is_passed_at_last=" . ($large_than ? 1 : 0);
						echo (db_exec($sql_1)) ? $alert_str[0] : $alert_str[1];
					}
					// 当流程还将继续的时候
					else
					{
						// 更新表 document：
						$sql_1 = "UPDATE document SET exam_level=" . ($exam_level + 1) . " WHERE doc_id=" . $doc_id;
						// 添加数据到 doc_exam： 
						$sql_2 = "INSERT INTO doc_exam(doc_id, handout_id, opn_id, content, has_examed, exam_level, exam_time) "
							. " VALUES(" . $doc_id . ", " . $n_to_id . ", 0, '', 0, " . ($exam_level + 1) . ", '0000-00-00')";
						echo (db_exec($sql_1) && db_exec($sql_2)) ? $alert_str[0] : $alert_str[1];
					}
				}
			}
		}
	}
}

// 当 $page_status == 2 时，提交下一位审批人数据
elseif ($page_status == 2)
{
   include("../../tools/mysql.php");
   db_connect();
   
   $doc_id = $_POST["doc_id"];
   $next_receiver = $_POST["next_receiver"];
   $is_multi = $_POST["is_multi"];
   
   $sql = "SELECT * FROM document WHERE $doc_id=" . $doc_id;
   $re = db_query_once($sql);
   
   $exam_level = $re["exam_level"];
   
   if ($is_multi == 1)
	 {
		$sql_m = "UPDATE document SET to_id=" . $next_receiver . " WHERE doc_id=" . $doc_id;
		echo (db_exec($sql_m)) ? "Success: 数据提交成功！" : "Error: 数据提交失败！";
	 }
   else
	 {
		$sql1 = "UPDATE document SET to_id=" . $next_receiver . ", exam_level=" . ($exam_level + 1) . " WHERE doc_id=" . $doc_id;
		$sql2 = "INSERT INTO doc_exam(doc_id, handout_id, has_examed, exam_level) ";
		$sql2 .= " VALUES(" . $doc_id . ", " . $next_receiver . ", 0, " . ($exam_level + 1) . ")";
		echo (db_exec($sql1) && db_exec($sql2)) ? "Success: 数据提交成功！" : "Error: 数据提交失败！";
	 }
}
   
?>
